package cn.edu.scut.fsearch.util;

import java.io.IOException;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import jeasy.analysis.MMAnalyzer;


public class HighLight {
	public static String doHighLight(String text,String keywords,String className)
	{
		String result = text;
		MMAnalyzer analyzer = new MMAnalyzer();
		String [] fckeyWord = null;
		try {
			fckeyWord = analyzer.segment(keywords, "@@@").split("@@@");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("find error when do highlight with "+keywords);
		}
		if(fckeyWord == null)
			return text;
		
		for (String string : fckeyWord) {
			result = result.replaceAll("(?i)"+Pattern.quote(string), "<span class=\""+className+"\">"+string+"</span>");
			//result = StringUtils.replaceChars(result, string, "<span class=\""+className+"\">"+string+"</span>");
		}
		return result;
	}
}
